﻿using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http.HttpResults;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using Novel.Server.Models;
using Novel.Server.Response;

namespace Novel.Server.Controllers
{
    /// <summary>
    /// 负责显示图片
    /// </summary>
    [Route("api/image")]
    [ApiController]
    public class ImageController : ControllerBase
    {
        private readonly NovelContext dbContext;
        private readonly IConfiguration conf;

        public ImageController(NovelContext context, IConfiguration configuration)
        {
            dbContext = context;
            conf = configuration;
        }

        /// <summary>
        /// 获取一张图片
        /// </summary>
        [HttpGet("{id}")]
        public async Task<IActionResult> GetImage(Guid id)
        {
            var image = await dbContext.Images.FirstOrDefaultAsync(i => i.ImageId == id);

            if (image == null)
                return NotFound(ApiResponse.ErrorResponse("Image not found", 404));

            return File(image.Content, image.Mime);
        }
    }
}

